package btc123.spider;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import btc123.entity.Btc123;
import jodd.http.HttpRequest;
import jodd.jerry.Jerry;
import jodd.jerry.JerryFunction;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SpiderBtc123 {
	private final String url = "http://news.btc123.com/news/ajax?page=1";

	public List<Btc123> spider() {
		String html = HttpRequest.get(url).send().bodyText();
		Jerry doc = Jerry.jerry(html);

		List<Btc123> btcs = new ArrayList<Btc123>();
		doc.$("div#ershouli ul li").each(new JerryFunction() {
			public Boolean onNode(Jerry $this, int index) {
				try {
					Btc123 btc = new Btc123();
					// 获取表面一层的,第一个div
					Jerry newsPic = $this.$("div.n_newspic img");
					String title = newsPic.attr("alt");// 标题
					btc.setTitle(title);
					String titlePic = newsPic.attr("src");// 标题图片
					btc.setTitlePic(titlePic);
					// 获取表面一层的,第二个div
					Jerry newsContent = $this.$("div.n_newscontent");
					// 主体内容,需要再次跳转获取
					String contentHref = newsContent.$("div.n_newstitle a").attr("href");
					String contentUrlFinal = "http://news.btc123.com" + contentHref;// 拼接完整的跳转url
					String content = getContent(contentUrlFinal);// 二次跳转,获取新闻内容
					btc.setContent(content);
					Jerry newsIcon = newsContent.$("div.n_newsicon");
					String dateFubu = newsIcon.$("span.n_newsdate").text().trim();// 发布日期
					String origin = newsIcon.$("span.slogo a").text().trim();// 来源
					btc.setOrigin(origin);

					Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateFubu);

					btc.setDateFubu(parse);

					btcs.add(btc);
					return true;
				}catch (ParseException e) {
					log.error("日期解析失败..停止!!",e);
					return false;
				} catch (Exception e) {
					log.error("解析发生异常!..停止!!",e);
					return false;
				}
			}
		});
		return btcs;
	}

	private String getContent(String contentUrlFinal) {
		String html = HttpRequest.get(contentUrlFinal).send().bodyText();
		Jerry doc = Jerry.jerry(html);
		
		return null;
	}

	public static void main(String[] args) {
		new SpiderBtc123().spider();
	}
}
